﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Commonlib;
using Albon.Model;

namespace DAL
{
    public class UserDAL
    {
        //UserAccount

        //验证用户名和密码
        public bool checkAccount(string name,string psw)
        {
            string cmdstr = "select count(*) from UserAccount where userName='"+Common.sqlDefend(name)
                +"' and userPsw='"+Common.sqlDefend(psw)+"' and active=1";
            int count=int.Parse(SqlHelper.ExecuteScalar(SqlHelper.SqlConnectionString, CommandType.Text,cmdstr, null).ToString());
            return count > 0;
        }

        //获取当前用户信息
        public  DataTable getUser(string limit)
        {
            string cmdstr = "select * from UserView where " + limit;
            return SqlHelper.ExecuteDataSet(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).Tables[0];

        }
        /// <summary>
        /// 添加新用户/用户名是不可以修改的
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool alterUser(UserAccount user)
        {
            SqlParameter[] parameters = new SqlParameter[]{
                    new SqlParameter("@ID",user.ID),
                    new SqlParameter("@roleID",user.roleID),
					new SqlParameter("@userName",user.userName),
					new SqlParameter("@userPsw",user.userPsw),
                    new SqlParameter("@payMethod", user.payMethod),
					new SqlParameter("@shipMethod",user.shipMethod),
                    new SqlParameter("@maxUser", user.maxUser),
					new SqlParameter("@credible",user.credible),
					new SqlParameter("@active",user.active),
                    new SqlParameter("@remark",user.remark)
            };
            int ret = SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "p_useralter", parameters);
            return ret > 0;
        }
        
        /// <summary>
        /// 修改用户信息
        /// </summary>
        public bool userDetails(UserAccount user)
        {
            SqlParameter[] parameters = new SqlParameter[] {
                new SqlParameter("@ID",SqlDbType.Int,4),
                new SqlParameter("@trueName", SqlDbType.VarChar,50),
				new SqlParameter("@countryID", SqlDbType.Int,4),
				new SqlParameter("@companyName", SqlDbType.VarChar,50),
				new SqlParameter("@phoneNo", SqlDbType.VarChar,50),
				new SqlParameter("@email", SqlDbType.VarChar,50),
				new SqlParameter("@userAddress", SqlDbType.VarChar,500),
            };
            parameters[0].Value = user.ID;
            parameters[1].Value = user.trueName;
            parameters[2].Value = user.countryID;
            parameters[3].Value = user.companyName;
            parameters[4].Value = user.phoneNo;
            parameters[5].Value = user.email;
            parameters[6].Value = user.userAddress;
            SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString,CommandType.StoredProcedure,"p_userdetails",parameters);
            return true;              
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        public bool delUser(int id)
        {
            string cmdstr = "delete from userAccount where ID="+id;
            return SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString,CommandType.Text,cmdstr,null)>0;
        }
    }
}
